#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'Dell'
__mtime__ = '2018-01-05'
# code is far away from bugs 
"""

import re


def Intoduct():
    # 将正则表达式编译成pattern对象，注意hello前面的r表示“原生字符串
    pattern = re.compile('hello')

    # 使用re.match匹配文本,获得匹配结果，无法匹配时返回none
    result1 = re.match(pattern, 'hello')
    result2 = re.match(pattern, 'helloo CQC!')
    result3 = re.match(pattern, 'helo CQC')
    result4 = re.match(pattern, 'hello CQC')

    # 如果1匹配成功
    if result1:
        # 使用match获取分组信息
        print(result1.group())
    else:
        print('result1匹配失败')

    if result2:
        print(result2.group())
    else:
        print('result2匹配失败')

    if result3:
        print(result3.group())
    else:
        print('result3匹配失败')

    if result4:
        print(result4.group())
    else:
        print('result4匹配失败')


def MatchAttr():
    # 匹配如下内容：单词+空格+单词+任意字符
    m = re.match(r'(\w+) (\w+)(?P<sign>.*)', 'hello world!')

    print "m.string:", m.string
    print "m.re:", m.re
    print "m.pos:", m.pos
    print "m.endpos:", m.endpos
    print "m.lastindex:", m.lastindex
    print "m.lastgroup:", m.lastgroup
    print "m.group():", m.group()
    print "m.group(1,2):", m.group(1, 2)
    print "m.groups():", m.groups()
    print "m.groupdict():", m.groupdict()
    print "m.start(2):", m.start(2)
    print "m.end(2):", m.end(2)
    print "m.span(2):", m.span(2)
    print r"m.expand(r'\g \g\g'):", m.expand(r'\2 \1\3')

    ### output ###
    # m.string: hello world!
    # m.re:
    # m.pos: 0
    # m.endpos: 12
    # m.lastindex: 3
    # m.lastgroup: sign
    # m.group(1,2): ('hello', 'world')
    # m.groups(): ('hello', 'world', '!')
    # m.groupdict(): {'sign': '!'}
    # m.start(2): 6
    # m.end(2): 11
    # m.span(2): (6, 11)
    # m.expand(r'\2 \1\3'): world hello!


def SearchPattern():
    # 将正则表达式编译成Pattern对象
    pattern = re.compile(r'world')
    # 使用search()查找匹配的子串，不存在能匹配的子串时将返回None
    # 这个例子中使用match()无法成功匹配
    match = re.search(pattern, 'hello world!')
    if match:
        # 使用Match获得分组信息
        print match.group()
    ### 输出 ###
    # world


def SplitPattern():
    pattern = re.compile(r'\d+')
    print re.split(pattern, 'one1two2three3four4')

    ### 输出 ###
    # ['one', 'two', 'three', 'four', '']


def FindAllPattern():
    pattern = re.compile(r'\d+')
    print re.findall(pattern, 'one1two2three3four4')

    ### 输出 ###
    # ['1', '2', '3', '4']


def Finditer():
    pattern = re.compile(r'\d+')
    for m in re.finditer(pattern, 'one1two2three3four4'):
        print m.group(),

    ### 输出 ###
    # 1 2 3 4


# sub
pattern = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'

print re.sub(pattern, r'\2 \1', s)


def func(m):
    return m.group(1).title() + ' ' + m.group(2).title()


print re.sub(pattern, func, s)

### output ###
# say i, world hello!
# I Say, Hello World!


# subn
pattern = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'

print re.subn(pattern, r'\2 \1', s)


def func(m):
    return m.group(1).title() + ' ' + m.group(2).title()


print re.subn(pattern, func, s)

### output ###
# ('say i, world hello!', 2)
# ('I Say, Hello World!', 2)


if __name__ == '__main__':
    MatchAttr()
